﻿//创建时间: 2011-8-8
//创建作者: 李城
//功能说明: CMS系统 模板文件管理
//修改: 2011-8-8
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.Common;
using TRPOP.CMS.Web.UI;


using TRPOP.CMS.Business;
using TRPOP.CMS.Model;
using Newtonsoft.Json;
using TRPOP.CMS.Plugin;
using TRPOP.Cache;


namespace TRPOP.CMS.Admin
{
    public partial class FileControl : BaseAdmin
    {
        #region 当前页面权限验证
        protected override void SubPageInit()
        {
            base.m_strPagePermissionExtraKey = "plugin_TemplateControl";
        }
        #endregion

        #region 页面变量
        protected string m_strRoot = "";
        protected string m_strsyntax = "html";//文件类型
        #endregion

        #region 页面事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadBase();
                ExecuteJs("IniteditArea()");
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ViewState["file"] != null)
            {
                m_strRoot = ViewState["file"].ToString();
            }
            if (VersionLimit.IsDemo)
            {
                ExecuteJs("IniteditArea();alert('Demo版不支持编辑的功能');");
                return;
            }
            else if (ViewState["file"] != null && FileUtil.IsExist(Common.Util.GetMapPath(ViewState["file"].ToString())))
            {
                FileUtil.WriteFile(Common.Util.GetMapPath(ViewState["file"].ToString()), ContentValue.Text);
                ICache oICache = ICacheAccess.GetInstance();
                oICache.RemoveByRegexp(CacheKeys.Template_AllKeys);
                if (FileUtil.IsExist(Common.Util.GetMapPath(ViewState["file"].ToString()) + ".cs"))
                {
                    FileUtil.DelFile(Common.Util.GetMapPath(ViewState["file"].ToString()) + ".cs");
                }
                if (FileUtil.IsExist(Common.Util.GetMapPath(ViewState["file"].ToString()) + ".dll"))
                {
                    FileUtil.DelFile(Common.Util.GetMapPath(ViewState["file"].ToString()) + ".dll");
                }
                ExecuteJsWidthEnd("alert('模板已保存');parent.close_dialog()");
            }
            else
            {
                ExecuteJsWidthEnd("alert('文件不存在');parent.close_dialog()");
                return;
            }
        }
        #endregion

        #region 页面方法

        public void LoadBase()
        {
            string file = "/" + RequestHelper.RequestString("file");
            file = file.Replace("//", "/");
            if (string.IsNullOrEmpty(file))
            {
                Alert("请通过正常途径进入本页面");
                return;
            }
            if (!FileUtil.IsExist(Common.Util.GetMapPath(file)))
            {
                Alert("文件不存在!");
                return;
            }
            try
            {
                string strFileHtml = FileUtil.ReadFile(Common.Util.GetMapPath(file));
                ViewState["file"] = file;
                ContentValue.Text = strFileHtml;
                m_strRoot = file;
            }
            catch (Exception oex)
            {
                Alert("读取文件失败!" + oex.Message);
                return;
            }
            switch (file.Substring(file.LastIndexOf('.') + 1).ToLower())
            {
                case "htm":
                case "html":
                    m_strsyntax = "html";
                    break;
                case "css":
                    m_strsyntax = "css";
                    break;
                case "js":
                    m_strsyntax = "js";
                    break;
                case "xml":
                case "conifg":
                    m_strsyntax = "xml";
                    break;
                default:
                    break;
            }
        }
        #endregion
    }
}
